import markdown
import re
from services import MyCache

class MarkDown():
    cache = MyCache().fsession()
    # markdown转html
    @cache.memoize(expire=86400,tag="markdownToStr")
    def mark(self,strs):
        exts = ['markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.tables',
                'markdown.extensions.toc']
        return markdown.markdown(strs,extensions=exts)

    # markdown提取文本(desc)
    def toText(self,strs):
        txt = self.mark(strs)
        dr = re.compile(r'<[^>]+>', re.S)
        dd = dr.sub('', txt)
        return dd[0:120]

    def clearCache(self):
        cache = MyCache().fsession()
        cache.evict("markdownToStr")

